/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.senac.controle;

import br.com.senac.dao.SituacaoReservaDAO;
import br.com.senac.dao.SituacaoReservaDAOImp;
import br.com.senac.entidades.SituacaoReserva;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Alessandra
 */
@WebServlet(name = "ControleSituacaoReserva", urlPatterns = {"/cnt.situacaoReserva"})
public class ControleSituacaoReserva extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private HttpServletRequest request;
    private HttpServletResponse response;
    private SituacaoReserva situacaoReserva;
    private RequestDispatcher rd;

    protected void processRequest()
            throws ServletException, IOException {

        String cmd = request.getParameter("cmd");

        switch (cmd) {
            case "salvar":
                salvar();
                break;
            case "listar":
                listar();
                break;
            case "excluir":
                excluir();
                break;
            case "pesqSituacaoR":
                pesquisaSituacaoR();
                break;

            case "alterar":
                alterar();
                break;
        }

    }

    private void salvar() {
        SituacaoReservaDAO contSrDAO = new SituacaoReservaDAOImp();
        try {
            contSrDAO.salvar(carregaSituacaoR());
            listar();
        } catch (Exception ex) {
            Logger.getLogger(ControleSituacaoReserva.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void listar() {
        SituacaoReservaDAO contSrDAO = new SituacaoReservaDAOImp();

        try {
            List situacaoR = contSrDAO.listar();
            request.setAttribute("listaStReserva", situacaoR);
            rd = request.getRequestDispatcher("mostraSituacaoReserva.jsp");
            rd.forward(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ControleSituacaoReserva.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void excluir() {
        SituacaoReservaDAO situacaoR = new SituacaoReservaDAOImp();
        String idCmd = request.getParameter("id");
        int id = Integer.parseInt(idCmd);
        try {
            situacaoR.excluir(id);
            listar();
        } catch (Exception ex) {
            Logger.getLogger(ControleSituacaoReserva.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void pesquisaSituacaoR() {
        SituacaoReservaDAO situacaoR = new SituacaoReservaDAOImp();
        String idForm = request.getParameter("id");
        int id = Integer.parseInt(idForm);

        try {
            situacaoReserva = (SituacaoReserva) situacaoR.procurar(id);
            request.setAttribute("situacaoR", situacaoReserva);
            rd = request.getRequestDispatcher("formAtuSituacaoR.jsp");
            rd.forward(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ControleSituacaoReserva.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void alterar() {
        SituacaoReservaDAO situacaoR = new SituacaoReservaDAOImp();
        try {
            situacaoR.alterar(carregaSituacaoR());
            listar();
        } catch (Exception ex) {
            Logger.getLogger(ControleSituacaoReserva.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private SituacaoReserva carregaSituacaoR() {
        situacaoReserva = new SituacaoReserva();
        String id = request.getParameter("id");
        if (id != null) {
            situacaoReserva.setId(Integer.parseInt(id));
        }
        situacaoReserva.setStatus(request.getParameter("status"));
        return situacaoReserva;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.request = request;
        this.response = response;

        processRequest();
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.request = request;
        this.response = response;

        processRequest();
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}